<?php
namespace App\Model;

use Carbon\Carbon;
use Illuminate\Support\Facades\DB;

class ChatFriend extends BaseModel
{
    protected $table = 'chat_friend_baseinfo';//国庆送礼记录表
    protected $userTable = 'user';//用户表
    protected $unionTble = 'union';//公会表
    protected $unionUserTable = 'union_user';//公会成员表
    protected $connection = 'api';//数据库地址
    protected $activityConnection = 'activity';//活动数据库

   public function getChatFriendList($page,$limit,$status,$user_id){
        $where = function($q)use($status,$user_id) {  
            //审核类型
            if($status >=0){
                $q->where('chat_friend_baseinfo.status',$status);
            }
            if(!empty($user_id)){
                $q->where('chat_friend_baseinfo.user_id',$user_id);
            }
        };
        $res = DB::connection($this->connection)
             ->table($this->table)
             ->leftJoin($this->userTable,'user.user_id','chat_friend_baseinfo.user_id')
             ->leftJoin($this->unionUserTable,'chat_friend_baseinfo.user_id','union_user.user_id')
             ->leftJoin($this->unionTble, "union.id", "=", "union_user.un_id")
             ->where('union.authentication_status','>',0)
             ->where($where)
             ->paginate($limit,['chat_friend_baseinfo.*','user.uuid','user.nickname','union.name as union_name'],'page',$page);
             
        $res = objToArray($res);
        if(!empty($res['data'])){
            foreach($res['data'] as $k=>&$v){
                $v['voice'] = env('OSS_URL').$v['voice'];
                $v['image'] =  build_image($v['image']);
            }
        }
        return $res;
   }
   
   //审核
   public function chageStatus($id,$status){
    $input = [
        'id' => $id,
        'status' => $status 
    ];
    return curl_request(getKkaudioApiUrl() . '/inner/user/changechatfriend', $input, 'json', 'POST');
   }
}